'''
Created on May 25, 2012

@author: thomas_dao
'''

from web import base
from web.base import BaseHandler
from web import urls
from entity.entity import EventInvite, Event
from web import util
from google.appengine.ext import ndb
from webapp2_extras.appengine.auth.models import User
import json
import logging

class CreateHandler(BaseHandler):
    @base.user_required
    def post(self):
        """
        @param message: to invite others
        @param event_id: id of post or event, e.g: 15
        @param invitee_ids: array of invitees, e.g: [1, 3, 4] 
        }
        """
        invite = EventInvite()
        invite.inviter = util.get_current_user_key()
        invite.message = self.request.params.get('message')
        
        # Get event
        event_id = self.request.params.get('event_id')
        event_key = ndb.Key(Event, int(event_id))
        
        invite.event = event_key
        # Get list of invitees
        invitee_ids = json.loads(self.request.params.get('invitee_ids'))
        invitee_key_list = []
        for invitee_id in invitee_ids:
            invitee_key = ndb.Key(User, int(invitee_id))
            invitee_key_list.append(invitee_key)

        invite.invitees = invitee_key_list
        
        invite.put()
        
        
        # Write response back
        self.response.write(json.dumps(util.ndb_to_dict(invite)))
    

class ReadHandler(BaseHandler):
    def get(self):
        pass    

class UpdateHandler(BaseHandler):
    def post(self):
        pass
    

class DeleteHandler(BaseHandler):
    def post(self):
        pass    
        

app = urls.app

def main():
    app.run()

if __name__ == '__main__':
    main()